'''
Author: aggfb 714740235@qq.com
Date: 2024-05-26 18:20:37
LastEditors: aggfb 714740235@qq.com
LastEditTime: 2024-06-04 22:37:56
FilePath: \XDrive-masterd:\devlop\fan\4.软件工具\bin_converter-master\binary_converter.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''

#!/usr/bin/env python3

import argparse

#******************************************************************************
#
# Read in the binary files and output the merged binary
#
#******************************************************************************
def process(app_filename, bin_size, bin_value, output):
    # Open the file, and read it into an array of integers.
    with open(app_filename, mode = 'rb') as f_app:
        app_binarray = f_app.read()
        f_app.close()

    app_length  = len(app_binarray)
    print("app_size ",hex(app_length), "(",app_length,")")

    pad_binarray = bytearray([])
    pad_size = 0
    if (app_length > 0):
        if (app_length < bin_size):
            # Add Padding
            pad_size = bin_size - app_length
            pad_binarray = bytearray([0]*pad_size); 

            for i in range(len(pad_binarray)):
                pad_binarray[i] = bin_value

            # now output all three binary arrays in the proper order
            with open(output + '.bin', mode = 'wb') as out:
                out.write(app_binarray)
                if (pad_size != 0):
                    # Add Padding
                    print('Adding padding of {} bytes...'.format(pad_size), flush=True)
                    print('bin_size of {} bytes...'.format(bin_size), flush=True)
                    out.write(pad_binarray)
        else:
            print('input --size is smaller than app binary file size.')


def parse_arguments():
    parser = argparse.ArgumentParser(description =
                     'Create a custom sized binary file .')

    parser.add_argument('--appbin', dest='appbin', default='FOC_xinaqi_20210512_0xB7D3.bin',
                        help='Application binary file (app.bin)') 
    
    # add arg binary type
    parser.add_argument('--size', dest='bin_size', default='0x4000',
                        help = 'Binary size')
                                                    
    # add arg binary type
    parser.add_argument('--value', dest='bin_value', default='0xff',
                        help = 'need fill a value in binary file.')

    parser.add_argument('-o', dest = 'output', default = 'main_16K',
                        help = 'Output filename (without the extension)')

    args = parser.parse_args()

    args.bin_size = int(args.bin_size, 16);
    args.bin_value = int(args.bin_value, 16);
    print('args.bin_size {} bytes...'.format(args.bin_size), flush=True)
    print('args.bin_value {} bytes...'.format(args.bin_value), flush=True)

    return args

#******************************************************************************
#
# Main function.
#
#******************************************************************************
def main():
    # Read the arguments.
    args = parse_arguments()
 
    process(args.appbin, args.bin_size, args.bin_value, args.output)

if __name__ == '__main__':
    main()
